package com.chpro.jdbc;


import javax.sql.DataSource;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 数据源帮助类
 * @author Administrator
 *
 */
public class DBUtil {
	
	private static CompositeConfiguration config = new CompositeConfiguration();
	private static BasicDataSource dataSource = null;
	private static JdbcTemplate jdbcTemplate = new JdbcTemplate();
	static{	//初始化信息
		
		//初始化配置文件
		try {
			config.addConfiguration(new PropertiesConfiguration("jdbc.properties"));
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//初始化数据源
		dataSource = new BasicDataSource(); 
		dataSource.setUrl(config.getString("jdbc.url"));
		dataSource.setDriverClassName(config.getString("jdbc.driverClassName"));
		dataSource.setUsername(config.getString("jdbc.username"));
		dataSource.setPassword(config.getString("jdbc.password"));
		
		//初始化JdbcTemplate
		jdbcTemplate.setDataSource(dataSource);
	}
	
	/**
	 * 得到一个数据源
	 */
	public static DataSource getDataSource(){
		return dataSource;
		
	}
	
	/**
	 * 得到一个JdbcTemplate
	 */
	public static JdbcTemplate getJdbcTemplate(){
		return jdbcTemplate;
	}
}
